Day 12 - Regular expressions - Single
characters
– You know, you got to keep regular if you want to be happy.
The Shining (1980)
Some time ago, I needed to review around 1000 files to check if IP numbers were mentioned in them.
I wasn’t looking for a specific number, the requirement was to find IP numbers in general. Trying
all of them was absolutely out of the question, as IPv4 has a pool of 4 billion possible addresses.
At 1 address per second it would take a little bit more than 136 years, and while I am still in my
forties I somehow doubt that I have that amount of time. Especially to spend finding IP addresses!
Fortunately I know regular expressions, and the whole task took me approximately 1 minute.
Regular expressions (regex) are the most powerful tool you can learn when it comes to searching
(and replacing) text. They are strings, with a specific syntax, that are used to search other strings.
They unfortunately have a reputation of being incredibly complicated, and I hope I will succeed in
debunking this myth. You can definitely come up with complex regular expressions, and sometimes
it can take you a while to figure out how to express a specific pattern, but you will see that they are
definitely accessible.
Did I manage to instill hope you? Yes? Good, because this chapter will be incredibly difficult. Just
kidding!
To practice regular expressions we are going to use the file examples.txt and our good friend grep.
Let’s see grep in action with the option -E that enables regular expressions
$ grep -E "dog" examples.txt
dog
dog
corn dog
So, apparently the option -E doesn’t change the tool’s behaviour, so disappointing. This is because
the string dog is already a valid regular expression, namely the one that searches for the character
d, followed by the character o, followed by the character g. You are probably thinking that I’m just
beating around the bush, right?
Well, time to write the first “proper” regular expression, or at least to use some of the special syntax
that regular expressions provide. After all, we are not here to just see cats and dogs. Let’s run the
following command (remember there is no -o option this time)